package com.winning.pmph.aspect;

import com.winning.pmph.interceptor.KmsCacheInterceptor;
import com.winning.pmph.utils.ApplicationContextUtil;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

@Aspect
@Component
public class RefreshCacheAspect {
    @After("@annotation(org.springframework.cache.annotation.CacheEvict)")
    public void refreshCacheId() {
        KmsCacheInterceptor.refreshCacheId();
        Cookie cacheIdCookie = new Cookie("cacheId", KmsCacheInterceptor.cacheId);
        cacheIdCookie.setPath("/");
        HttpServletResponse response = ApplicationContextUtil.getResponse();
        if (Objects.nonNull(response)) {
            response.addCookie(cacheIdCookie);
        }
    }
}
